/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TinTuc;

import java.io.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

/**
 *
 * @author Admin
 */
public abstract class XPath {

    protected String xmlFile;
    protected String rootroot;
    protected String root;
    protected String link;
    protected String title;
    protected String img;
    protected String date;
    protected String summary;
    protected org.jdom2.Element eDomain;
    protected static String keyDomain;

    public String getKeyDomain() {
        return keyDomain;
    }

    public void setKeyDomain(String keyDomain) {
        XPath.keyDomain = keyDomain;
    }

    public int Count() throws FileNotFoundException, ParserConfigurationException, SAXException, IOException {
        try {
            FileInputStream file = new FileInputStream(new File(this.xmlFile));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(file);

            javax.xml.xpath.XPath xPath = XPathFactory.newInstance().newXPath();
            String expression = this.rootroot;
            NodeList node = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
            return node.getLength();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SAXException | ParserConfigurationException | XPathExpressionException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public String getLink() throws FileNotFoundException, ParserConfigurationException, SAXException, IOException {
        try {
            FileInputStream file = new FileInputStream(new File(this.xmlFile));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(file);

            javax.xml.xpath.XPath xPath = XPathFactory.newInstance().newXPath();
            String expression = this.link;
            Element node = (Element) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
            String tmp = node.getAttribute("href");
            return tmp;
        } catch (FileNotFoundException e) {
        } catch (SAXException | ParserConfigurationException | XPathExpressionException e) {
        } catch (IOException e) {
        }
        return null;

    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getTitle() {
        try {
            FileInputStream file = new FileInputStream(new File(this.xmlFile));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(file);

            javax.xml.xpath.XPath xPath = XPathFactory.newInstance().newXPath();
            String expression = this.title;
            Node node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
            String tmp = node.getTextContent();
            return tmp;
        } catch (FileNotFoundException e) {
        } catch (SAXException | ParserConfigurationException | XPathExpressionException e) {
        } catch (Exception e) {
        }
        return null;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getImg() {
        try {
            FileInputStream file = new FileInputStream(new File(this.xmlFile));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(file);

            javax.xml.xpath.XPath xPath = XPathFactory.newInstance().newXPath();
            String expression = this.img;
            Element node = (Element) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
            String tmp = node.getAttribute("src");
            return tmp;
        } catch (FileNotFoundException e) {
        } catch (SAXException | ParserConfigurationException | XPathExpressionException e) {
        } catch (Exception e) {
        }
        return null;

    }

    public void setImg(String img) {
        this.img = img;
    }

    public String getDate() {
        try {
            FileInputStream file = new FileInputStream(new File(this.xmlFile));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(file);

            javax.xml.xpath.XPath xPath = XPathFactory.newInstance().newXPath();
            String expression = this.date;
            Node node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
            String tmp = node.getTextContent();
            return tmp;
        } catch (FileNotFoundException e) {
        } catch (SAXException | ParserConfigurationException | XPathExpressionException e) {
        } catch (Exception e) {
        }
        return null;

    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getSummary() {
        try {
            FileInputStream file = new FileInputStream(new File(this.xmlFile));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(file);

            javax.xml.xpath.XPath xPath = XPathFactory.newInstance().newXPath();
            String expression = this.summary;
            Node node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
            String tmp = node.getTextContent();
            return tmp;
        } catch (FileNotFoundException e) {
        } catch (SAXException | ParserConfigurationException | XPathExpressionException e) {
        } catch (Exception e) {
        }
        return null;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getXmlFile() {
        return xmlFile;
    }

    public void setXmlFile(String xmlFile) {
        this.xmlFile = xmlFile;
    }

    public String getRoot() {
        return root;
    }

    public void setRoot(String root) {
        this.root = root;
    }
}
